import Vue from "vue";
import VueRouter from "vue-router";
import Login from "@/views/Login.vue";
import Register from "@/views/Register.vue";
import Layout from "@/views/Layout.vue";
import Students from "@/views/Students.vue";
import Index from "@/views/Index.vue";

import store from "@/store";

Vue.use(VueRouter);

const routes = [
  {
    path: "/login",
    component: Login,
  },
  {
    path: "/register",
    component: Register,
  },
  {
    path: "/",
    component: Layout,
    redirect: "/index",
    children: [
      {
        path: "/index",
        component: Index,
      },
      {
        path: "/students",
        component: Students,
      },
    ],
  },
];

const router = new VueRouter({
  routes,
});

// 白名单
const whileList = ["/login", "/register"];
//路由守卫
router.beforeEach((to, form, next) => {
  if (store.state.user.token || whileList.includes(to.path)) {
    next();
  } else {
    next("/login");
  }
});

export default router;
